var app = getApp()
var host = app.data.host
var httpUtil = require("../utils/httpUtil.js")
var payUtil = require('../utils/payUtil.js')
var cartUtil = require("../utils/cartUtil.js")
var createGoodsOrderUrl = host + "createGoodsOrder" //创建商品订单
var createTuanOrderUrl = host + 'createTuanOrder' //创建拼团订单
var createRechargeOrderUrl = host + 'createRechargeOrder' //创建充值订单
var updateOrderUrl = host + "updateOrderStatus" //更新订单状态
var updateTuanOrderUrl = host + "updateTuanOrderStatus" //更新订单状态
var joinTuanUrl = host + 'joinTuan' //入团
var orderListUrl = host + "orderList" //订单列表
var payOrderUrl = host + "payOrder" //待付款订单直接付款
var deleteOrderUrl = host + "deleteOrder" //删除订单
var refundCauseUrl = host + "getRefundCause" //退款原因列表
var refundApplyUrl = host + "refundApply" //退款申请
var evaluateUrl = host + "evaluate" //评价
var getOrderEvaluateUrl = host + "getOrderEvaluates"
var orderInfoUrl = host + "getOrderInfo" //订单详情
//创建商品订单
function createGoodsOrder(obj, list, cb) {
  var user = app.data.user
  obj.cusId=user.id,
  obj.cusName=user.name,
  obj.openid=user.openid
  list = handleList(list)
  obj.goods = JSON.stringify(list)
  httpUtil.post(createGoodsOrderUrl, obj, res => {
    if (res.status == 0) { //创建订单成功
      cartUtil.removes(list)
      payUtil.sendPay(res.data, status => {
        if (status) { //支付成功
          updatePayStatus(res.data.orderId, cb)
        } else {
          cb({
            status: 1,
            msg: "支付失败"
          })
        }
      })
    } else {
      cb(res)
    }
  })
}
//创建拼团订单
function createTuanOrder(obj, cb) {
  var user = app.data.user
  var data = {
    cusId: user.id,
    cusName: user.name,
    openid: user.openid,
    tuanId: obj.tuanId,
  }
  httpUtil.post(createTuanOrderUrl, data, res => {
    if (res.status == 0) { //创建订单成功
      payUtil.sendPay(res.data, status => {
        if (status) { //支付成功
          updateTuanOrderStatus(res.data.orderId, cb)
        } else {
          cb({
            status: 1,
            msg: "支付失败"
          })
        }
      })
    } else {
      cb(res)
    }
  })
}

function joinTuanOrder(obj, cb) {
  var user = app.data.user
  var data = {
    cusId: user.id,
    cusName: user.name,
    openid: user.openid,
    tuanId: obj.tuanId,
    tuanRecordId: obj.tuanRecordId,
  }
  httpUtil.post(createTuanOrderUrl, data, res => {
    if (res.status == 0) { //创建订单成功
      payUtil.sendPay(res.data, status => {
        if (status) { //支付成功
          joinTuan(res.data.orderId, cb)
        } else {
          cb({
            status: 1,
            msg: "支付失败"
          })
        }
      })
    } else {
      cb(res)
    }
  })
}
//创建余额充值订单
function createRechargeOrder(money, cb) {
  var user = app.data.user
  var data = {
    cusId: user.id,
    cusName: user.name,
    totalPrice: money,
    reqPrice: money,
    openid: user.openid,
  }
  httpUtil.post(createRechargeOrderUrl, data, res => {
    if (res.status == 0) { //创建订单成功
      payUtil.sendPay(res.data, status => {
        if (status) { //支付成功
          updatePayStatus(res.data.orderId, cb)
        } else {
          cb({
            status: 1,
            msg: "支付失败"
          })
        }
      })
    } else {
      cb(res)
    }
  })
}
//用户订单列表
function getOrderList(cb) {
  var data = {
    cusId: app.data.user.id
  }
  httpUtil.get(orderListUrl, data, cb)
}

//更新支付状态
function updatePayStatus(id, cb) {
  httpUtil.get(updateOrderUrl, {
    orderId: id
  }, cb)
}
//更新拼团订单
function updateTuanOrderStatus(id, cb) {
  var data = {
    orderId: id,
    cusId: app.data.user.id
  }
  httpUtil.get(updateTuanOrderUrl, data, res => {
    cb(res)
  })
}
//加入团
function joinTuan(id, cb) {
  var data = {
    orderId: id,
    cusId: app.data.user.id
  }
  httpUtil.get(joinTuanUrl, data, res => {
    cb(res)
  })
}
//处理商品集合
function handleList(list) {
  var arr = []
  for (var i = 0; i < list.length; i++) {
    var obj = list[i]
    var temp = {
      pid: obj.id,
      pname: obj.name,
      type: obj.type,
      num: obj.num,
      price: obj.price,
      payPrice: obj.price * obj.num,
      imgUrl: obj.imgUrl,
    }
    arr.push(temp)
  }
  return arr
}
//待支付订单-支付
function payOrder(id, type, cb) {
  var data = {
    orderId: id,
    openid: app.data.user.openid
  }
  httpUtil.get(payOrderUrl, data, res => {
    if (res.status == 0) { //创建订单成功
      payUtil.sendPay(res.data, status => {
        if (status) { //支付成功
          if (type == 1) updateTuanOrderStatus(res.data.orderId, cb)
          else if (type == 2) joinTuan(res.data.orderId, cb)
          else updatePayStatus(res.data.orderId, cb)
        } else {
          cb({
            status: 1,
            msg: "支付失败"
          })
        }
      })
    } else {
      cb(res)
    }
  })
}
//删除订单
function deleteOrder(id, cb) {
  httpUtil.get(deleteOrderUrl, {
    orderId: id
  }, cb)
}
//退款评价
function refundCause(cb) {
  httpUtil.get(refundCauseUrl, null, cb)
}
//退款申请
function refundApply(data, cb) {
  httpUtil.post(refundApplyUrl, data, cb)
}
//评价
function evaluate(data, cb) {
  data.cusId = app.data.user.id
  httpUtil.post(evaluateUrl, data, cb)
}
//获取订单评价
function getOrderEvaluates(orderId, cb) {
  var data = {
    orderId: orderId
  }
  httpUtil.get(getOrderEvaluateUrl, data, cb)
}
//订单详情
function getOrderInfo(id, cb) {
  var data = {
    id: id
  }
  httpUtil.get(orderInfoUrl, data, cb)
}
module.exports = {
  createGoodsOrder: createGoodsOrder,
  createRechargeOrder: createRechargeOrder,
  createTuanOrder: createTuanOrder,
  joinTuanOrder: joinTuanOrder,
  getOrderList: getOrderList,
  payOrder: payOrder,
  deleteOrder: deleteOrder,
  refundCause: refundCause,
  refundApply: refundApply,
  evaluate: evaluate,
  getOrderEvaluates: getOrderEvaluates,
  getOrderInfo: getOrderInfo
}